/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TOrder;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TOrderDao;
import com.ruipengkj.service.TOrderService;

/**
 * 说明： 定单Service层接口实现
 * 
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TOrderServiceImpl implements TOrderService {
	@Resource
	private TOrderDao TOrderDao;// DAO层

	public static void main(String[] args) {

	}

	// 查询所有
	@Override
	public List<TOrder> findService(TOrder entity) throws Exception {
		String hql = "select o from TOrder o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if (entity.getTUser() != null && !entity.getTUser().equals("")) {
				whereHQL.append("userid").append(BaseQuery.EQ)
						.append("'" + entity.getTUser().getUserID() + "'")
						.append(BaseQuery.AND);
				// param.add(entity.getTUser().getUserID());
			}
			if (entity.getSource() != 100 && entity.getSource() != 0) {
				whereHQL.append("source=?").append(BaseQuery.AND);
				param.add(entity.getSource());
			}
			if (entity.getStatus() != 100 && entity.getStatus() != 0) {
				whereHQL.append("status=?").append(BaseQuery.AND);
				param.add(entity.getStatus());
			}
			if (entity.getPayStatus() != 0) {
				whereHQL.append("payStatus=?").append(BaseQuery.AND);
				param.add(entity.getPayStatus());
			}
			if (entity.getPosID() != null && !entity.getPosID().equals("")) {
				whereHQL.append("posID=?").append(BaseQuery.AND);
				param.add(entity.getPosID());
			}
			if (whereHQL.length() > 4)
				whereHQL.delete(whereHQL.length() - 5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TOrderDao.findDao(hql, whereHQL.toString(), param, orderby);
	}

	//查询未付款的订单
	@Override
	public List<TOrder> findPayStatus(TOrder entity) throws Exception {
		String hql = "select o from TOrder o";
		StringBuffer whereHQL = new StringBuffer() ;
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if (entity.getPayStatus() != 0) {
				whereHQL.append("payStatus=?");//.append(BaseQuery.AND);
				param.add(entity.getPayStatus());
			}
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TOrderDao.findDao(hql, whereHQL.toString(), param, orderby);
	}

	
	// 查询所有
	@Override
	public List<TOrder> findByDeskService(TOrder entity) throws Exception {
		String hql = "select o from TOrder o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if (entity.getPayStatus() != 0) {
				whereHQL.append("payStatus=?").append(BaseQuery.AND);
				param.add(entity.getPayStatus());
			}
			if (entity.getPosID() != null) {
				whereHQL.append("posID=?").append(BaseQuery.AND);
				param.add(entity.getPosID());
			} else {
				whereHQL.append("o.posID is null").append(BaseQuery.AND);
			}
			if (whereHQL.length() > 4)
				whereHQL.delete(whereHQL.length() - 5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TOrderDao.findDao(hql, whereHQL.toString(), param, orderby);
	}

	// 根据批次号查询
	public List<TOrder> findByWorkShiftID(TOrder entity) {
		String hql = "select o from TOrder o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if (entity.getWorkShiftID() != null
					&& !entity.getWorkShiftID().equals("")) {
				whereHQL.append("WorkShiftID").append(BaseQuery.EQ).append("?")
						.append(BaseQuery.AND);
				param.add(entity.getWorkShiftID());
			}

			if (whereHQL.length() > 4)
				whereHQL.delete(whereHQL.length() - 5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TOrderDao.findDao(hql, whereHQL.toString(), param, orderby);
	}

	// 根据ID翻页查询
	@Override
	public PageModel<TOrder> getScrollService(int firstResult, int maxResult,
			TOrder entity) throws Exception {
		String hql = "select o from TOrder o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if (entity.getOrderID() != null && !entity.getOrderID().equals("")) {
				whereHQL.append("orderID").append(BaseQuery.LIKE).append("?")
						.append(BaseQuery.AND);
				param.add("%" + entity.getOrderID() + "%");
			}
			if (entity.getSaler() != null && !entity.getSaler().equals("")) {
				whereHQL.append("saler").append(BaseQuery.LIKE).append("?")
						.append(BaseQuery.AND);
				param.add("%" + entity.getSaler() + "%");
			}
			if (entity.getSource() != 100) {
				whereHQL.append("source=?").append(BaseQuery.AND);
				param.add(entity.getSource());
			}
			if (entity.getStatus() != 100) {
				whereHQL.append("status=?").append(BaseQuery.AND);
				param.add(entity.getStatus());
			}
			// 按时间查询
			if (null != entity.getHappenTime()
					&& !("").equals(entity.getHappenTime())) {
				whereHQL.append("HappenTime>=?").append(BaseQuery.AND);
				param.add(entity.getHappenTime());
			}
			if (null != entity.getEndTime()
					&& !("").equals(entity.getEndTime())) {
				whereHQL.append("HappenTime<=?").append(BaseQuery.AND);
				param.add(entity.getEndTime());
			}
			// 用户数据
			if (entity.getTUser() != null) {
				if (entity.getTUser().getUserName() != null
						&& !entity.getTUser().getUserName().equals("")) {
					whereHQL.append("TUser.userName").append(BaseQuery.LIKE)
							.append("?").append(BaseQuery.AND);
					param.add("%" + entity.getTUser().getUserName() + "%");
				}
				if (entity.getTUser().getMobile() != null
						&& !entity.getTUser().getMobile().equals("")) {
					whereHQL.append("TUser.mobile").append(BaseQuery.LIKE)
							.append("?").append(BaseQuery.AND);
					param.add("%" + entity.getTUser().getMobile() + "%");
				}
			}
			if (whereHQL.length() > 4)
				whereHQL.delete(whereHQL.length() - 5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TOrderDao.getScrollDao(hql, whereHQL.toString(), param,
				firstResult, maxResult, orderby);
	}

	// 根据是否被orderid
	public List<TOrder> findOrderid(TOrder entity) {
		String hql = "select o from TOrder o";
		StringBuffer whereHQL = new StringBuffer() ;
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if (entity.getOrderID() != null && !entity.getOrderID().equals("")) {
				whereHQL.append("orderID").append(BaseQuery.LIKE).append("?")
						.append(BaseQuery.AND);
				param.add("%" + entity.getOrderID() + "%");
			}
			
//			if (whereHQL.length() > 4)
//				whereHQL.delete(whereHQL.length() - 5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TOrderDao.findDao(hql, whereHQL.toString(), param, orderby);

	}
	
	// 修改
	@Override
	public void modifyService(TOrder entity) throws Exception {
		TOrderDao.modifyDao(entity);

	}

	// 添加
	@Override
	public void saveService(TOrder entity) throws Exception {
		TOrderDao.saveDao(entity);
	}

	// 根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TOrderDao.deleteDao(id);

	}

	// 根据ID查询
	@Override
	public TOrder getByIdService(String id) throws Exception {
		return TOrderDao.getByIdDao(id);
	}

	// 添加或修改
	@Override
	public void saveOrUpdateService(TOrder entity) throws Exception {
		TOrderDao.saveOrUpdateDao(entity);
	}

	// 添加返回ID
	@Override
	public Serializable saveRetService(TOrder entity) throws Exception {
		return TOrderDao.saveRetDao(entity);
	}

	@Override
	public Double findCardAmountByWorkShiftID(String WorkShiftID) {
		return TOrderDao.findCardAmountByWorkShiftID(WorkShiftID);
	}

	@Override
	public Double findCashAmountByWorkShiftID(String WorkShiftID) {
		return TOrderDao.findCashAmountByWorkShiftID(WorkShiftID);
	}

	@Override
	public Double findDiscountAmountByWorkShiftID(String WorkShiftID) {
		return TOrderDao.findDiscountAmountByWorkShiftID(WorkShiftID);
	}

	@Override
	public Integer findOrderCountByWorkShiftID(String WorkShiftID) {
		return TOrderDao.findOrderCountByWorkShiftID(WorkShiftID);
	}

	public TOrderDao getTOrderDao() {
		return TOrderDao;
	}

	public void setTOrderDao(TOrderDao TOrderDao) {
		this.TOrderDao = TOrderDao;
	}

}